package com.xzy.urp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzy.urp.pojo.entity.BaseInfo;
import com.xzy.urp.mapper.BaseInfoMapper;
import com.xzy.urp.pojo.vo.BaseInfoVO;
import com.xzy.urp.pojo.vo.CourseTimeVO;
import com.xzy.urp.service.BaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzy
 * @since 2022-02-17
 */
@Service
public class BaseInfoServiceImpl extends ServiceImpl<BaseInfoMapper, BaseInfo> implements BaseInfoService {

    @Override
    public List<CourseTimeVO> getTimeByBaseId(Integer baseId) {
        // todo: 加缓存
        return baseMapper.getTimeByBaseId(baseId);
    }

    @Override
    public List<BaseInfoVO> getAddableBase(Integer semester, Integer section, Integer start, Integer end) {

        return baseMapper.getAddableBase(semester,section,start,end);
    }

    @Override
    public List<BaseInfoVO> getBaseInfoByBuild(String build) {
        QueryWrapper<BaseInfo> wrapper = new QueryWrapper<BaseInfo>().eq("building", build);
        List<BaseInfo> baseInfos = baseMapper.selectList(wrapper);
        ArrayList<BaseInfoVO> baseInfoVOS = new ArrayList<>();
        baseInfos.forEach(e->{
            BaseInfoVO baseInfoVO = new BaseInfoVO();
            BeanUtil.copyProperties(e,baseInfoVO);
            baseInfoVOS.add(baseInfoVO);
        });
        return baseInfoVOS;
    }

    @Override
    public List<BaseInfoVO> listAll() {

        List<BaseInfo> baseInfos = baseMapper.selectList(null);
        ArrayList<BaseInfoVO> baseInfoVOS = new ArrayList<>();
        baseInfos.forEach(e->{
            BaseInfoVO baseInfoVO = new BaseInfoVO();
            BeanUtil.copyProperties(e,baseInfoVO);
            baseInfoVOS.add(baseInfoVO);
        });
        return baseInfoVOS;
    }

    @Override
    public Boolean isAddable(Integer semester, Integer section, Integer start, Integer end, Integer baseId) {
        Integer count = baseMapper.countAddableById(semester, section, start, end, baseId);
        //等于1表示可置入
        return  count==1;
    }
}
